Results 1 to 5 of 5

Thread: How to make a “loading…” window appear only once when a specific file is found infin

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to make a “loading…” window appear only once when a specific file is found infin

    I want to display a "generating image..." kind of modal dialog, other than the main GUI. This "generating image..." dialog should be temporary, and be displayed and disappear without user intervention. For displaying this dialog, the Qt code should check for existence of a usbResponse.txt file in a specific location in the PC's hard disk. If the usbResponse.txt file exists, then the dialog should pop-up. I am able to display this loading GIF.
    I am able to close the GIF whenever that txt file i.e. usbResponse.txt is modified, using FileSystemWatcher.

    The issue is when I'm scanning for the existence of the .txt using threads. When I find the file I display the loading GIF. But due to it's continuous nature, the GIF windows are displayed multiple times. I want this to happen only once, when it's found. Would something like 'break' work? I find it unlikely. How to do this?Please advise. Here is my code:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFile>
    6. #include <QDebug>
    7. #include <QFileSystemWatcher>
    8. #include "dialog.h"
    9. #include "mythread.h"
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22.  
    23.  
    24. public slots:
    25. void afterFileHasBeenFound();
    26. void closeModified(const QString &str);
    27.  
    28. private slots:
    29.  
    30. private:
    31. Ui::MainWindow *ui;
    32. Dialog *pDialog;
    33. MyThread *mThread;
    34. };
    35.  
    36. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QtCore>
    6. #include <QDebug>
    7.  
    8. #define FILE_PATH "E:\\QT1\\dialogClose2\\dialogClose2\\usbResponse.txt"
    9.  
    10. class MyThread : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit MyThread(QObject *parent = 0);
    15. void run();
    16. QString name;
    17. int exec();
    18. void checkFile();
    19.  
    20. signals:
    21. void testSignal(QString message);
    22. void fileFoundDisplayGif();
    23.  
    24. public slots:
    25.  
    26. };
    27.  
    28. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QMovie>
    6. #include <QLabel>
    7.  
    8. #define GIF_PATH "E:\\QT1\\timeStampPopUp\\timeStampPopUp\\loading.gif"
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20. void displayLoadingGif();
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24. };
    25.  
    26. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. displayLoadingGif();
    10. }
    11.  
    12. Dialog::~Dialog()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Dialog::displayLoadingGif()
    18. {
    19. QMovie *pMovie = new QMovie(GIF_PATH);
    20. ui->loadingGifLabel->setMovie(pMovie);
    21. pMovie->start();
    22. }
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2.  
    3. MyThread::MyThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. }
    7.  
    8. void MyThread::run()
    9. {
    10. exec();
    11. }
    12.  
    13. int MyThread::exec()
    14. {
    15. while(1)
    16. {
    17. checkFile();
    18. emit(testSignal("hello world!!"));
    19. sleep(1);
    20. }
    21. }
    22.  
    23. void MyThread::checkFile()
    24. {
    25. QFile file(FILE_PATH);
    26. if(file.exists())
    27. {
    28. qDebug()<<"exists";
    29. emit(fileFoundDisplayGif());
    30. }
    31. else
    32. qDebug()<<"doesn't exist";
    33. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. mThread = new MyThread(this);
    10. mThread->name = "mThread";
    11. connect(mThread, SIGNAL(fileFoundDisplayGif()), this, SLOT(afterFileHasBeenFound()), Qt::QueuedConnection);
    12. mThread->start();
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::afterFileHasBeenFound()
    21. {
    22. pDialog = new Dialog();
    23. pDialog->setModal(true);
    24. pDialog->show();
    25. }
    26.  
    27. void MainWindow::closeModified(const QString &str)
    28. {
    29. Q_UNUSED(str)
    30. pDialog->hide();
    31. QFile file(FILE_PATH);
    32. file.remove();
    33. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QFileSystemWatcher fileWatcher;
    8. fileWatcher.addPath(FILE_PATH);
    9. QStringList fileList = fileWatcher.files();
    10. Q_FOREACH(QString file, fileList)
    11. qDebug() << "File name " << file;
    12. MainWindow* mc = new MainWindow;
    13. QObject::connect(&fileWatcher, SIGNAL(fileChanged(QString)), mc, SLOT(closeModified(QString)));
    14. mc->show();
    15.  
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to make a “loading…” window appear only once when a specific file is found in

    Why do you let the thread continue if once you found the file?
    Why do you use a thread at all?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make a “loading…” window appear only once when a specific file is found in

    Hi! The file will come anytime, I need to keep on scanning for this file. So once this file is found, i cannot stop, i have to delete this file and keep scanning until another such file comes. Hope this clarified your doubt.

    Quote Originally Posted by anda_skoa View Post
    Why do you let the thread continue if once you found the file?
    Why do you use a thread at all?

    Cheers,
    _
    Last edited by Sai Kamat; 14th March 2014 at 15:16.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to make a “loading…” window appear only once when a specific file is found in

    Any reason you don't use QFileSystemWatcher?

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make a “loading…” window appear only once when a specific file is found in

    Yes, coz QFileSystemWatcher is very generic. I want it to monitor for the existence of a particular file only. QFileSystemWatcher will monitor the entire directory and emit signal for any modification in the directory. I do not desire this.

Similar Threads

  1. Replies: 2
    Last Post: 7th February 2017, 11:03
  2. make: command not found, Error: 127
    By ct in forum General Programming
    Replies: 3
    Last Post: 21st April 2015, 08:19
  3. Replies: 4
    Last Post: 7th August 2011, 15:44
  4. make[2]:arm-linux-g++:Command not found.(Greenphone SDK)
    By rishiraj in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 17th April 2009, 13:57
  5. QT4 for kde 4 beta 2 configure problem: make not found
    By marcomangiante in forum Installation and Deployment
    Replies: 1
    Last Post: 5th September 2007, 20:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.